<?php
namespace Misc;
include_once APPPATH . '/lib/PhpMailer/class.phpmailer.php';
/**
 * File holds class for misc methods (tools) used in application
 *
 * PHP version 5.3.5
 *
 * @category   CM
 * @package    Misc
 * @subpackage -
 * @author     markus karileet <markuskarileet@hotmail.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @link       -
 */
class Tools {
	/**
	 * Method tries to autoload called class
	 * @param var $classname
	 */
	public static function autoload($classname)
	{
		//autoload application classes
		if (strstr($classname, '\\')) {
			$file = CLASSPATH . str_replace('\\', '/', $classname) . '.php';
			if (file_exists($file)) {
				require_once  $file;
			}
		} else {
			$file = CLASSPATH . $classname . '.php';
			if (file_exists($file)) {
				require_once  $file;
			//try logger classes
			} else {
				$file = LOGGERDIR . $classname . '.php';
				if (file_exists($file)) {
					require_once  $file;
				}
			}
		}
	}
	
	/**
	 * Method requires and initiates smarty
	 * @return Smarty Template object
	 */
	public static function initSmarty()
	{
		require_once  SMARTYDIR . 'Smarty.class.php';
		$smarty = new \Smarty();
		$smarty->template_dir = TEMPLATES.'templates';
		$smarty->compile_dir = TEMPLATES.'templates_c';
		$smarty->cache_dir = TEMPLATES.'cache';
		$smarty->config_dir = TEMPLATES.'configs';
		return $smarty;
	}
	
	/**
	 * Method requires and initializes PHPExcel component
	 * @return \PHPExcel
	 */
	public static function initPhpExcel() {
		require_once PHPEXCELDIR . 'PHPExcel.php';
		return new \PHPExcel();
	}
	
	/**
	 * Method requires and initializes PHPExcel Excel2007 writer
	 * @return \PHPExcel
	 */
	public static function initPhpExcelWriter($obj) {
		require_once PHPEXCELDIR . 'PHPExcel/Writer/Excel2007.php';
		return new \PHPExcel_Writer_Excel2007($obj);
	}

	/**
	 * Method initiates log4php class. Configuration is included when found in
	 * configs
	 */
	public static function initLogger() {
		//configure only when file is found
		\LoggerMDC::put('user', @$_SESSION['userid']);
		$file = CLASSPATH . 'Configs/dailyfile.properties';
		if (file_exists($file)) {
			\Logger::configure($file);
		}
		return \Logger::getLogger('TableTalk');
	}
	
	/**
	 * Method removes tags, escapes quotes and removes whitespace
	 * @param var $str unescaped string
	 * @return var Escaped string
	 */
	public static function cleanInput($str) {
		$out = strip_tags($str);
		$out = addslashes($out);
		$out = trim($out);
		return $out;
	}
	
	/**
	 * Method sends Email. Admins that have notify flag, receive BCC
	 * @param unknown_type $to
	 * @param unknown_type $subject
	 * @param unknown_type $body
	 * @param unknown_type $altBody
	 */
	public static function sendEmail($to, $subject, $body, $altBody) {
		$mail = new \PHPMailer();
		$mail->From = 'no-reply@cmweb.planet.ee';
		$mail->FromName = 'TableTalk';
		
		$mail->AddAddress($to);
		$accountService = new \Services\Account();
		$admins = $accountService->getAdminsToNotify();
		//check if there are any admins :P
		if ($admins) {
			foreach (array_keys($admins) as $key) {
				$mail->AddBCC($admins[$key]->getEmail());
			}
		}
		$mail->IsHTML(true);
		$mail->CharSet = 'utf-8';
		$mail->Subject = $subject;
		$mail->Body    = $body;
		$mail->AltBody = $altBody;
		
		return $mail->Send();
	}
}